<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2008 Christophe Monard <contact@cmonard.fr>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

/**
 * Librairie de compression de fichier CSS
 *
 * @author	Christophe Monard   <contact@cmonard.fr>
 *
 * [CLASS/FUNCTION INDEX of SCRIPT]
 *
 *   47: class tx_cmdapi_compressor
 *   56:    function css_compress(&$content, $caller)
 *   78:    private function compress($content)
 *
 * TOTAL FUNCTIONS: 2
 *
 */

/**
 * Plugin 'CMD API Compressor' for the 'cmd_api' extension.
 *
 * @author	Christophe Monard <contact@cmonard.fr>
 * @package	TYPO3
 * @subpackage	tx_cmdapi
 */
class tx_cmdapi_compressor {
	
	/**
	 * The main method of the PlugIn
	 *
	 * @param	array		$content: The CSS Array
	 * @param	object		$cller: The caller object
	 * @return	void
	 */
	function css_compress(&$content, $caller) {
		// traitons les inlines CSS
		foreach ($content['cssInline'] as $name => $conf)
			if ($conf['compress'] == 1)
				$content['cssInline'][$name]['code'] = $this->compress($conf['code']);
		// traitons les fichiers CSS
		foreach ($content['cssFiles'] as $file => $conf) {
			if ($conf['compress'] == 1) {
				$css = file_get_contents(PATH_site.$file);
				$css = $this->compress($css);
				file_put_contents(PATH_site.$file, $css);
			}
		}
	}

	/**
	 * The compress function
	 * 
	 * Not perfect and maybe can produce bug for some hack
	 * @param	string		$content: the CSS string to compress
	 * @return	string The compressed CSS string
	 */
	private function compress($content) {
		// remove comments
	    $content = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $content);
		// define the rules
		$str_replace = array(
			array('string' => array("\r\n"), 'replacement' => "\n"), //replace windows new line with linux new line
			array('string' => array("\t", '  ', '    ', '    '), 'replacement'  => ''), // remove surrounding spaces and tabs
			array('string' => array("\n{", ' {', '{ ', "{\n"), 'replacement'  => '{'), // remove new line and space before and after {
			array('string' => array(' }', "\n}", '}'), 'replacement'  => "}\n"), // remove new line and space before } and go to new line
			array('string' => array('; ', ";\n"), 'replacement'  => ';'), // remove space and new line after ;
			array('string' => array(': ', ' :'), 'replacement'  => ':'), // remove space before and after :
			array('string' => array(",\n", ', ', ' ,', "\n,"), 'replacement'  => ','), // remove new line and space before and after ,
			array('string' => array("\n\n"), 'replacement'  => "\n"), // remove multi-line new line by only one new line
		);
		// process the rules
		foreach ($str_replace as $replacement)
			$content = str_replace($replacement['string'], $replacement['replacement'], $content);
		// returning the trimmed result
		return trim($content);
	}
}

if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/cmd_api/class.tx_cmdapi_compressor.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/cmd_api/class.tx_cmdapi_compressor.php']);
}
?>